﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;
using Might.Infrastructure.DOM;
using Might.Infrastructure.Events;
using Might.Infrastructure.Services;

namespace Might.ProgressService {

    public class ProgressService : IProgressService {
        private readonly IEventAggregator eventAggregator;
        public ProgressService(IEventAggregator eventAggregator) {
            this.eventAggregator = eventAggregator;
        }

        public IProgressToken ProgressCreate(Type type, IDomNode iDomItem) {
            IProgressToken p = new ProgressToken() { EventType = type , iDomItem = iDomItem };
            eventAggregator.GetEvent<ProgressEvent>().Publish(p);
            return p;
        }
    }

    public class ProgressToken : IProgressToken {
        
        public Type EventType { get; set; }
        public IDomNode iDomItem { get; set; }

        public ProgressEventArgs State { get; internal set; }

        public event ProgressEventHandler ProgressGo;

        public void ProgressStart(string state) {
            State = new ProgressEventArgs(0, state);
            onProgressGo(this, State);
        }

        public void Progress(int percent, string state) {
            State = new ProgressEventArgs(percent, state);
            onProgressGo(this, State);
        }

        public void ProgressEnd(ProgressResult progressResult, string state) {
            State = new EndProgressEventArgs( 100 , state , progressResult );
            onProgressGo( this , State );
        }
        
        protected void onProgressGo( IProgressToken sender , ProgressEventArgs e ) {       
            if( ProgressGo != null ) {                
                ProgressGo( sender , e );
            }
        }
    }

}
